<?php
/**
 * EY Web Crawling : Step 1 : Employee List
 */

use Custom\Employee;
use Fox\Common\Localization\SubRegion;
use Fox\Common\Localization\City;
use Fox\Common\Localization\City\Street;
use Fox\Html\CheckBox;
use Fox\Html\Input;
use Fox\Html\Screen;

set_time_limit(Xview::TIMEOUT_VERY_LONG);

$screen = new Screen("extract","extract");
$block = new Block("b","Extract Source Files");
$block->add(new TextArea("comments",false));
$block->add(new CheckBox("commit"));
$block->add(new Input("dbcnt",true,1));
$screen->add($block);

if($screen->posted())
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		$mode = null;
		$rewrite = true;
		$first = true;
		$fragments[] = new Fox\Dao\SqlFragment("load_status = 0");
		$fragment = Fox\Dao\SqlFragment::combine($fragments);
		$finder = new Employee();
		$employees = $finder->find($fragment,array("reference" => false),null,$inputs["dbcnt"]);
		
		
		$mapper['0:0:1'] = 'civility';
		$mapper['0:3:1'] = 'first_name';
		$mapper['0:1:1'] = 'last_name';
		$mapper['0:2:1'] = 'last_name2';
		$mapper['0:4:1'] = 'birth_date';
		$mapper['0:6:1'] = 'adress';
		$mapper['0:7:1'] = 'zip_code';
		$mapper['0:8:1'] = 'city';
		$mapper['0:9:1'] = 'country';
		$mapper['0:0:3'] = 'phone1';
		$mapper['0:1:3'] = 'phone2';
		$mapper['0:2:3'] = 'email1';
		$mapper['0:4:3'] = 'start_on';
		$mapper['0:5:3'] = 'quit_on';
		$mapper['0:6:3'] = 'sector';
		$mapper['0:7:3'] = 'service_line';
		$mapper['0:8:3'] = 'bu';
		$mapper['0:9:3'] = 'status';
		$mapper['0:10:3'] = 'member';
		$mapper['0:11:2'] = 'school';
		$mapper['0:12:2'] = 'school_year';
		$mapper['3:0:1'] = 'company_name';
		$mapper['3:0:3'] = 'function';
		$mapper['3:1:1'] = 'company_sector';
		$mapper['3:3:3'] = 'job_phone1';
		$mapper['3:4:3'] = 'job_phone2';
		$mapper['3:2:3'] = 'job_email1';
		$mapper['3:3:1'] = 'company_adress';
		$mapper['3:4:1'] = 'company_zip_code';
		$mapper['3:5:1'] = 'company_city';
		$mapper['3:6:1'] = 'comapny_country';

		push_msg("I","Nb Employees Loaded :".count($employees));
		$errors = array();
		// Find City
		foreach($employees as $employee)
		{
			$fields = null;
			$targetFilename = _TEMP_."EY/Detail/".$employee->reference.".htm";
			if($mode == 'extract')
			{
				if(!file_exists($targetFilename))
				{
					echo "URL GOTO=https://emeadmz02.ey.com/marketing/fralumni.nsf/0/".$employee->reference."?Edit&Lang=FR";
					echo "<br>";
					echo "SAVEAS TYPE=CPL FOLDER=* FILE=*";
					echo "<br>";
					push_msg("S","File $targetFilename build");
				}
				else
				{
					push_msg("W","File $targetFilename already exist");
				}
				
			}
			else
			{
				if(!file_exists($targetFilename))
				{
					echo "URL GOTO=https://emeadmz02.ey.com/marketing/fralumni.nsf/0/".$employee->reference."?Edit&Lang=FR";
					echo "<br>";
					echo "SAVEAS TYPE=CPL FOLDER=* FILE=*";
					echo "<br>";
					continue;	
				}
				$html = file_get_html($targetFilename);
				$tables = $html->find("#tdHeight table");
				for($i=0;$i<4;$i++)
				{
					// Case 1 : Basic Information
					if($i == 0 || $i == 3)
					{
						$subTables = $tables[$i]->find("table");
						
						foreach($subTables as $subTable)
						{
							$lines = $subTable->find("tr");
							for($l=0;$l<count($lines);$l++)
							{
								$cells = $lines[$l]->find("td");
								for($c=0;$c<count($cells);$c++)
								{
									$key = "$i:$l:$c";
									if(isset($mapper[$key]))
									{
										$fields[$mapper[$key]] = trim(utf8_decode($cells[$c]->plaintext));
									}
								}
							}
						}
					}
				}
				
				try {
					$fields['load_status'] = 1;
					$employee->setFields($fields);
					if($inputs['commit'] && is_array($fields))
					{
						// Save in the DB
						$employee->update();
					}
					push_msg("I","Employee {$employee->reference} has been updated ");
				}
				catch(Fox\Object\Exception\Save $e)
				{
					push_msg("E",$e->getMessage());
				}
			}
		}
		
	}
}
echo $screen->display();
//https://emeadmz02.ey.com/marketing/fralumni.nsf/0/2210F5887B67476AC12579AE005BC25F?Edit&Lang=FR